package com.property.demo.repository;

import com.property.demo.model.UserRole;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository
public interface UserRoleRepository extends JpaRepository<UserRole, Long> {
    
    List<UserRole> findByUserId(Long userId);
    
    List<UserRole> findByRoleId(Long roleId);
    
    @Transactional
    @Modifying
    @Query("DELETE FROM UserRole ur WHERE ur.userId = ?1")
    void deleteByUserId(Long userId);
    
    @Transactional
    @Modifying
    @Query("DELETE FROM UserRole ur WHERE ur.roleId = ?1")
    void deleteByRoleId(Long roleId);
    
    @Transactional
    @Modifying
    @Query("DELETE FROM UserRole ur WHERE ur.userId = ?1 AND ur.roleId = ?2")
    void deleteByUserIdAndRoleId(Long userId, Long roleId);
} 